/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.model.Enhancement;
import org.crystalrange.service.EnhancementService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class EnhancementListController extends BaseController {

    protected EnhancementService enhancementService;
    protected Enhancement selectedEnhancement;
    protected Listbox listboxEnhancement;
    protected Search searchEnhancement;
    protected List<Enhancement> enhancements;

    public EnhancementService getEnhancementService() {
        return this.enhancementService;
    }

    public void setEnhancementService(EnhancementService enhancementService) {
        this.enhancementService = enhancementService;
    }

    public Enhancement getSelectedEnhancement() {
        return this.selectedEnhancement;
    }

    public void setSelectedEnhancement(Enhancement selectedEnhancement) {
        this.selectedEnhancement = selectedEnhancement;
    }

    public List<Enhancement> getEnhancements() {
        return this.enhancements;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchEnhancement = new Search(Enhancement.class);
        this.searchEnhancement.setMaxResults(100);
        this.searchEnhancement.addFetch("createdBy");
        this.searchEnhancement.addFetch("updatedBy");
        this.enhancements = this.enhancementService.search(this.searchEnhancement);

        component.setAttribute("controller", this, false);
    }

    public void onCreate$windowEnhancementList(Event event) throws Exception {
        this.binder = (AnnotateDataBinder) this.self.getAttribute("binder");
    }

    public void onClick$menuitemEnhancementCreate(Event event) throws Exception {
        this.enhancementCreate();
    }

    public void onClick$menuitemEnhancementEdit(Event event) throws Exception {
        this.enhancementEdit();
    }

    public void onClick$menuitemEnhancementDelete(Event event) throws Exception {
        this.enhancementDelete();
    }

    public void onClick$menuitemEnhancementSearch(Event event) throws Exception {
        this.enhancementSearch();
    }

    public void onClick$menuitemEnhancementRefresh(Event event) throws Exception {
        this.enhancementRefresh();
    }

    public void onClick$toolbarbuttonEnhancementCreate(Event event) throws Exception {
        this.enhancementCreate();
    }

    public void onClick$toolbarbuttonEnhancementEdit(Event event) throws Exception {
        this.enhancementEdit();
    }

    public void onClick$toolbarbuttonEnhancementDelete(Event event) throws Exception {
        this.enhancementDelete();
    }

    public void onClick$toolbarbuttonEnhancementSearch(Event event) throws Exception {
        this.enhancementSearch();
    }

    public void onClick$toolbarbuttonEnhancementRefresh(Event event) throws Exception {
        this.enhancementRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.enhancementEdit();
    }
    
    private void enhancementCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/enhancement.zul");
    }

    private void enhancementEdit() throws Exception {
        if (this.selectedEnhancement != null) {
            this.execution.sendRedirect("~./zul/html/enhancement.zul?Id=" + this.selectedEnhancement.getId().toString());
        }
    }

    private void enhancementDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxEnhancement.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    Enhancement enhancement = (Enhancement) selectedItem.getValue();
                    this.enhancementService.remove(enhancement);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.enhancements = this.enhancementService.search(this.searchEnhancement);
        this.binder.loadAll();
    }

    private void enhancementSearch() throws Exception {
        this.execution.setAttribute("searchEnhancement", this.searchEnhancement);

        Window window = (Window) this.execution.createComponents("~./zul/html/enhancementSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchEnhancement = (Search) this.execution.getAttribute("searchEnhancement");
        }

        this.enhancements = this.enhancementService.search(this.searchEnhancement);
        this.binder.loadAll();
    }

    private void enhancementRefresh() {
        this.enhancements = this.enhancementService.search(this.searchEnhancement);
        this.binder.loadAll();
    }
}
